<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="js," />





  <link rel="alternate" href="/atom.xml" title="OBKoro1's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="箭头函数是ES6的API，相信很多人都知道，因为其语法上相对于普通函数更简洁，深受大家的喜爱。就是这种我们日常开发中一直在使用的API，大部分同学却对它的了解程度还是不够深…">
<meta name="keywords" content="js">
<meta property="og:type" content="article">
<meta property="og:title" content="普通函数与箭头函数的区别">
<meta property="og:url" content="http://yoursite.com/2019/03/22/普通函数与箭头函数的区别/index.html">
<meta property="og:site_name" content="OBKoro1&#39;s Blog">
<meta property="og:description" content="箭头函数是ES6的API，相信很多人都知道，因为其语法上相对于普通函数更简洁，深受大家的喜爱。就是这种我们日常开发中一直在使用的API，大部分同学却对它的了解程度还是不够深…">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1g1b3rm317cj30go0b4aau.jpg?raw=true">
<meta property="og:updated_time" content="2019-07-20T06:55:39.061Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="普通函数与箭头函数的区别">
<meta name="twitter:description" content="箭头函数是ES6的API，相信很多人都知道，因为其语法上相对于普通函数更简洁，深受大家的喜爱。就是这种我们日常开发中一直在使用的API，大部分同学却对它的了解程度还是不够深…">
<meta name="twitter:image" content="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1g1b3rm317cj30go0b4aau.jpg?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/03/22/普通函数与箭头函数的区别/"/>





  <title>普通函数与箭头函数的区别 | OBKoro1's Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?64d57d0b926bbd27431daa89d2e0a76b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OBKoro1's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-前端算法">
          <a href="/arithmetic/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            前端算法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-博客最新更新">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            博客最新更新
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/22/普通函数与箭头函数的区别/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OBKoro1">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/hand1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OBKoro1's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">普通函数与箭头函数的区别</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-22T05:18:13+08:00">
                2019-03-22
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/js/" itemprop="url" rel="index">
                    <span itemprop="name">js</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2019/03/22/普通函数与箭头函数的区别/" class="leancloud_visitors" data-flag-title="普通函数与箭头函数的区别">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  3,472 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  13分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://github.com/OBKoro1/articleImg_src/blob/master/weibo_img_move/005Y4rCogy1g1b3rm317cj30go0b4aau.jpg?raw=true" alt=""></p>
<p>箭头函数是ES6的API，相信很多人都知道，因为其语法上相对于普通函数更简洁，深受大家的喜爱。就是这种我们日常开发中一直在使用的API，大部分同学却对它的了解程度还是不够深…</p>
<a id="more"></a>
<h2 id="普通函数和箭头函数的区别："><a href="#普通函数和箭头函数的区别：" class="headerlink" title="普通函数和箭头函数的区别："></a>普通函数和箭头函数的区别：</h2><h3 id="箭头函数的this指向规则："><a href="#箭头函数的this指向规则：" class="headerlink" title="箭头函数的this指向规则："></a>箭头函数的this指向规则：</h3><h4 id="1-箭头函数没有prototype-原型-，所以箭头函数本身没有this"><a href="#1-箭头函数没有prototype-原型-，所以箭头函数本身没有this" class="headerlink" title="1. 箭头函数没有prototype(原型)，所以箭头函数本身没有this"></a>1. 箭头函数没有<code>prototype</code>(原型)，所以箭头函数本身没有this</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="function"><span class="params">()</span> =&gt;</span>&#123;&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(a.prototype); <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>
<h4 id="2-箭头函数的this指向在定义的时候继承自外层第一个普通函数的this。"><a href="#2-箭头函数的this指向在定义的时候继承自外层第一个普通函数的this。" class="headerlink" title="2. 箭头函数的this指向在定义的时候继承自外层第一个普通函数的this。"></a>2. 箭头函数的this指向在定义的时候继承自外层第一个普通函数的this。</h4><p>下面栗子中在一个函数中定义箭头函数，然后在另一个函数中执行箭头函数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a,</span><br><span class="line">  barObj = &#123; <span class="attr">msg</span>: <span class="string">'bar的this指向'</span> &#125;;</span><br><span class="line">fooObj = &#123; <span class="attr">msg</span>: <span class="string">'foo的this指向'</span> &#125;;</span><br><span class="line">bar.call(barObj); <span class="comment">// 将bar的this指向barObj</span></span><br><span class="line">foo.call(fooObj); <span class="comment">// 将foo的this指向fooObj</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  a(); <span class="comment">// 结果：&#123; msg: 'bar的this指向' &#125;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bar</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  a = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>, <span class="string">'this指向定义的时候外层第一个普通函数'</span>); <span class="comment">// </span></span><br><span class="line">  &#125;; <span class="comment">// 在bar中定义 this继承于bar函数的this指向</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>从上面栗子中可以得出两点</p>
<ol>
<li><strong>箭头函数的this指向定义时所在的外层第一个普通函数，跟使用位置没有关系</strong>。</li>
<li><strong>被继承的普通函数的this指向改变，箭头函数的this指向会跟着改变</strong></li>
</ol>
<h4 id="3-不能直接修改箭头函数的this指向"><a href="#3-不能直接修改箭头函数的this指向" class="headerlink" title="3. 不能直接修改箭头函数的this指向"></a>3. 不能直接修改箭头函数的this指向</h4><p>上个栗子中的foo函数修改一下，尝试直接修改箭头函数的this指向。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> fnObj = &#123; <span class="attr">msg</span>: <span class="string">'尝试直接修改箭头函数的this指向'</span> &#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  a.call(fnObj); <span class="comment">// 结果：&#123; msg: 'bar的this指向' &#125;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>很明显，call显示绑定this指向失败了，包括aaply、bind都一样。</p>
<blockquote>
<p>它们(call、aaply、bind)会默认忽略第一个参数，但是可以正常传参。</p>
</blockquote>
<p>然后我又通过隐式绑定来尝试同样也失败了，new 调用会报错，这个稍后再说。</p>
<p>SO，<strong>箭头函数不能直接修改它的this指向</strong>。</p>
<p>幸运的是，我们可以通过间接的形式来修改箭头函数的指向：</p>
<p><strong>去修改被继承的普通函数的this指向，然后箭头函数的this指向也会跟着改变</strong>，这在上一个栗子中有演示。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bar.call(barObj); <span class="comment">// 将bar普通函数的this指向barObj 然后内部的箭头函数也会指向barObj</span></span><br></pre></td></tr></table></figure>
<h4 id="4-箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向window-全局对象"><a href="#4-箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向window-全局对象" class="headerlink" title="4. 箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向window(全局对象)"></a>4. 箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向<code>window</code>(全局对象)</h4><blockquote>
<p>唔，这个问题实际上是面试官提出来的，当时我认为的箭头函数规则就是：箭头函数的this指向继承自外层第一个普通函数的this，现在看来真是不严谨(少说一个定义的时候)，要是面试官问我：定义和执行不在同一个普通函数中，它又指向哪里，肯定歇菜…</p>
</blockquote>
<p>既然箭头函数的this指向在定义的时候继承自外层第一个普通函数的this，那么：</p>
<p><strong>当箭头函数外层没有普通函数，它的this会指向哪里</strong>？ </p>
<p>这里跟我之前写的<a href="https://juejin.im/post/5b3715def265da59af40a630#heading-3" target="_blank" rel="noopener">this绑定规则</a>不太一样(不懂的可以点进去看一下),普通函数的默认绑定规则是：</p>
<p>在非严格模式下，默认绑定的this指向全局对象，严格模式下this指向undefined</p>
<p><strong>如果箭头函数外层没有普通函数继承，它this指向的规则</strong>：</p>
<p>经过测试，箭头函数在全局作用域下，<strong>严格模式和非严格模式下它的this都会指向<code>window</code>(全局对象)</strong>。</p>
<p>Tip：测试的时候发现<strong>严格模式在中途声明无效，必须在全局/函数的开头声明才会生效</strong>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">1</span>;</span><br><span class="line"><span class="meta">'use strict'</span>; <span class="comment">// 严格模式无效 必须在一开始就声明严格模式</span></span><br><span class="line">b = <span class="number">2</span>; <span class="comment">// 不报错</span></span><br></pre></td></tr></table></figure>
<h3 id="箭头函数的"><a href="#箭头函数的" class="headerlink" title="箭头函数的"></a>箭头函数的</h3><h3 id="箭头函数的arguments"><a href="#箭头函数的arguments" class="headerlink" title="箭头函数的arguments"></a>箭头函数的arguments</h3><h4 id="箭头函数的this指向全局，使用arguments会报未声明的错误"><a href="#箭头函数的this指向全局，使用arguments会报未声明的错误" class="headerlink" title="箭头函数的this指向全局，使用arguments会报未声明的错误"></a>箭头函数的this指向全局，使用arguments会报未声明的错误</h4><p>如果箭头函数的this指向<code>window</code>(全局对象)使用<code>arguments</code>会报错，未声明<code>arguments</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> b = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">arguments</span>);</span><br><span class="line">&#125;;</span><br><span class="line">b(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>); <span class="comment">// Uncaught ReferenceError: arguments is not defined</span></span><br></pre></td></tr></table></figure>
<p>PS：如果你声明了一个全局变量为<code>arguments</code>，那就不会报错了，但是你为什么要这么做呢？</p>
<h4 id="箭头函数的this指向普通函数时-它的argumens继承于该普通函数"><a href="#箭头函数的this指向普通函数时-它的argumens继承于该普通函数" class="headerlink" title="箭头函数的this指向普通函数时,它的argumens继承于该普通函数"></a>箭头函数的this指向普通函数时,它的<code>argumens</code>继承于该普通函数</h4><p>上面是第一种情况：箭头函数的this指向全局对象，会报arguments未声明的错误。</p>
<p>第二种情况是：箭头函数的this如果指向普通函数,它的<code>argumens</code>继承于该普通函数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bar</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">arguments</span>); <span class="comment">// ['外层第二个普通函数的参数']</span></span><br><span class="line">  bb(<span class="string">'外层第一个普通函数的参数'</span>);</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">bb</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">arguments</span>); <span class="comment">// ["外层第一个普通函数的参数"]</span></span><br><span class="line">    <span class="keyword">let</span> a = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">arguments</span>, <span class="string">'arguments继承this指向的那个普通函数'</span>); <span class="comment">// ["外层第一个普通函数的参数"]</span></span><br><span class="line">    &#125;;</span><br><span class="line">    a(<span class="string">'箭头函数的参数'</span>); <span class="comment">// this指向bb</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">bar(<span class="string">'外层第二个普通函数的参数'</span>);</span><br></pre></td></tr></table></figure>
<p>那么应该如何来获取箭头函数不定数量的参数呢？答案是：ES6的rest参数（<code>...</code>扩展符）</p>
<h4 id="rest参数获取函数的多余参数"><a href="#rest参数获取函数的多余参数" class="headerlink" title="rest参数获取函数的多余参数"></a>rest参数获取函数的多余参数</h4><p>这是ES6的API，用于获取函数不定数量的参数数组，这个API是用来替代<code>arguments</code>的，API用法如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="function">(<span class="params">first, ...abc</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(first, abc); <span class="comment">// 1 [2, 3, 4]</span></span><br><span class="line">&#125;;</span><br><span class="line">a(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure>
<p>上面的栗子展示了，获取函数除第一个确定的参数，以及用一个变量接收其他剩余参数的示例。</p>
<p>也可以直接接收函数的所有参数，rest参数的用法相对于<code>arguments</code>的优点：</p>
<ol>
<li>箭头函数和普通函数都可以使用。</li>
<li>更加灵活，接收参数的数量完全自定义。</li>
<li><p>可读性更好</p>
<p> 参数都是在函数括号中定义的，不会突然出现一个<code>arguments</code>，以前刚见到的时候，真的好奇怪了！</p>
</li>
<li><p>rest是一个真正的数组，可以使用数组的API。</p>
<p> 因为<code>arguments</code>是一个类数组的对象，有些人以为它是真正的数组，所以会出现以下场景：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">arguments</span>.push(<span class="number">0</span>); <span class="comment">// arguments.push is not a function</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<pre><code>如上，如果我们需要使用数组的API，需要使用扩展符/Array.from来将它转换成真正的数组:

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">arguments</span> = [...arguments]; 或者 ：<span class="built_in">arguments</span> = <span class="built_in">Array</span>.from(<span class="built_in">arguments</span>);</span><br></pre></td></tr></table></figure>
</code></pre><p><strong>rest参数有两点需要注意</strong>：</p>
<ol>
<li><p>rest必须是函数的最后一位参数：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="function">(<span class="params">first, ...rest, three</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(first, rest,three); <span class="comment">// 报错：Rest parameter must be last formal parameter</span></span><br><span class="line">&#125;;</span><br><span class="line">a(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>函数的length属性，不包括 rest 参数</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">...a</span>) </span>&#123;&#125;).length  <span class="comment">// 0</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params">a, ...b</span>) </span>&#123;&#125;).length  <span class="comment">// 1</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>扩展运算符还可以用于数组，这里是阮一峰老师的<a href="http://es6.ruanyifeng.com/#docs/array#%E6%89%A9%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6" target="_blank" rel="noopener">文档</a></p>
<p>PS：感觉这里写多了，但比较喜欢把一个知识点讲清楚…</p>
<h4 id="使用new调用箭头函数会报错"><a href="#使用new调用箭头函数会报错" class="headerlink" title="使用new调用箭头函数会报错"></a>使用<code>new</code>调用箭头函数会报错</h4><p>无论箭头函数的thsi指向哪里，使用<code>new</code>调用箭头函数都会报错，因为箭头函数没有<code>constructor</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;;</span><br><span class="line"><span class="keyword">let</span> b = <span class="keyword">new</span>  a(); <span class="comment">// a is not a constructor</span></span><br></pre></td></tr></table></figure>
<h4 id="箭头函数不支持new-target："><a href="#箭头函数不支持new-target：" class="headerlink" title="箭头函数不支持new.target："></a>箭头函数不支持<code>new.target</code>：</h4><p><code>new.target</code>是ES6新引入的属性，普通函数如果通过<code>new</code>调用，<code>new.target</code>会返回该函数的引用。</p>
<p>此属性主要：用于确定构造函数是否为new调用的。</p>
<ol>
<li><p>箭头函数的this指向全局对象，在箭头函数中使用箭头函数会报错</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="keyword">new</span>.target); <span class="comment">// 报错：new.target 不允许在这里使用</span></span><br><span class="line">&#125;;</span><br><span class="line">a();</span><br></pre></td></tr></table></figure>
</li>
<li><p>箭头函数的this指向普通函数，它的new.target就是指向该普通函数的引用。</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> bb();</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bb</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> a = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">new</span>.target); <span class="comment">// 指向函数bb：function bb()&#123;...&#125;</span></span><br><span class="line">  &#125;;</span><br><span class="line">  a();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>更多关于<code>new.target</code>可以看一下阮一峰老师关于这部分的<a href="http://es6.ruanyifeng.com/?search=new.target&amp;x=0&amp;y=0#docs/class#new-target-%E5%B1%9E%E6%80%A7" target="_blank" rel="noopener">解释</a>。</p>
<h3 id="箭头函数不支持重命名函数参数-普通函数的函数参数支持重命名"><a href="#箭头函数不支持重命名函数参数-普通函数的函数参数支持重命名" class="headerlink" title="箭头函数不支持重命名函数参数,普通函数的函数参数支持重命名"></a>箭头函数不支持重命名函数参数,普通函数的函数参数支持重命名</h3><p>如下示例，普通函数的函数参数支持重命名，后面出现的会覆盖前面的，箭头函数会抛出错误：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func1</span>(<span class="params">a, a</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a, <span class="built_in">arguments</span>); <span class="comment">// 2 [1,2]</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> func2 = <span class="function">(<span class="params">a,a</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a); <span class="comment">// 报错：在此上下文中不允许重复参数名称</span></span><br><span class="line">&#125;;</span><br><span class="line">func1(<span class="number">1</span>, <span class="number">2</span>); func2(<span class="number">1</span>, <span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<h3 id="箭头函数相对于普通函数语法更简洁优雅："><a href="#箭头函数相对于普通函数语法更简洁优雅：" class="headerlink" title="箭头函数相对于普通函数语法更简洁优雅："></a>箭头函数相对于普通函数语法更简洁优雅：</h3><p>讲道理，语法上的不同，也属与它们两个的区别！</p>
<ol>
<li><p>箭头函数都是匿名函数，并且都不用写<code>function</code></p>
</li>
<li><p>只有一个参数的时候可以省略括号:</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> f = <span class="function"><span class="params">a</span> =&gt;</span> a; <span class="comment">// 传入a 返回a</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol start="3">
<li><p>函数只有一条语句时可以省略<code>{}</code>和<code>return</code></p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> f = <span class="function">(<span class="params">a,b,c</span>) =&gt;</span> a; <span class="comment">// 传入a,b,c 返回a</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>简化回调函数，让你的回调函数更优雅：</p>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].map(<span class="function"><span class="keyword">function</span> (<span class="params">x</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x * x;</span><br><span class="line">&#125;); <span class="comment">// 普通函数写法 </span></span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].map(<span class="function"><span class="params">x</span> =&gt;</span> x * x); <span class="comment">// 箭头函数只需要一行</span></span><br></pre></td></tr></table></figure>
<hr>
<h2 id="箭头函数的注意事项及不适用场景"><a href="#箭头函数的注意事项及不适用场景" class="headerlink" title="箭头函数的注意事项及不适用场景"></a>箭头函数的注意事项及不适用场景</h2><h3 id="箭头函数的注意事项"><a href="#箭头函数的注意事项" class="headerlink" title="箭头函数的注意事项"></a>箭头函数的注意事项</h3><ol>
<li><p>一条语句返回对象字面量，需要加括号，或者直接写成多条语句的<code>return</code>形式，</p>
<p> 否则像func中演示的一样，花括号会被解析为多条语句的花括号，不能正确解析</p>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> func1 = <span class="function"><span class="params">()</span> =&gt;</span> &#123; <span class="attr">foo</span>: <span class="number">1</span> &#125;; <span class="comment">// 想返回一个对象,花括号被当成多条语句来解析，执行后返回undefined</span></span><br><span class="line"><span class="keyword">var</span> func2 = <span class="function"><span class="params">()</span> =&gt;</span> (&#123;<span class="attr">foo</span>: <span class="number">1</span>&#125;); <span class="comment">// 用圆括号是正确的写法</span></span><br><span class="line"><span class="keyword">var</span> func2 = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    foo: <span class="number">1</span> <span class="comment">// 更推荐直接当成多条语句的形式来写，可读性高</span></span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>箭头函数在参数和箭头之间不能换行！</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> func = ()</span><br><span class="line">           =&gt; <span class="number">1</span>;  <span class="comment">// 报错： Unexpected token =&gt;</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li>箭头函数的解析顺序相对靠前</li>
</ol>
<p>MDN: 虽然箭头函数中的箭头不是运算符，但箭头函数具有与常规函数不同的特殊<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Operator_Precedence" target="_blank" rel="noopener">运算符优先级</a>解析规则</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="literal">false</span> || <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;&#125;; <span class="comment">// ok</span></span><br><span class="line"><span class="keyword">let</span> b = <span class="literal">false</span> || <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;; <span class="comment">// Malformed arrow function parameter list</span></span><br><span class="line"><span class="keyword">let</span> c = <span class="literal">false</span> || <span class="function">(<span class="params">(</span>) =&gt;</span> &#123;&#125;); <span class="comment">// ok</span></span><br></pre></td></tr></table></figure>
<h3 id="箭头函数不适用场景："><a href="#箭头函数不适用场景：" class="headerlink" title="箭头函数不适用场景："></a>箭头函数不适用场景：</h3><p>围绕两点：箭头函数的this意外指向和代码的可读性。</p>
<ol>
<li>定义字面量方法,this的意外指向。</li>
</ol>
<p>因为箭头函数的简洁</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  array: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],</span><br><span class="line">  sum: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 根据上文学到的：外层没有普通函数this会指向全局对象</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.array.push(<span class="string">'全局对象下没有array，这里会报错'</span>); <span class="comment">// 找不到push方法</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line">obj.sum();</span><br></pre></td></tr></table></figure>
<p>上述栗子使用普通函数或者ES6中的方法简写的来定义方法，就没有问题了：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这两种写法是等价的</span></span><br><span class="line">sum() &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>.array.push(<span class="string">'this指向obj'</span>);</span><br><span class="line">&#125;</span><br><span class="line">sum: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>.array.push(<span class="string">'this指向obj'</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>还有一种情况是给普通函数的原型定义方法的时候，通常会在普通函数的外部进行定义，比如说继承/添加方法的时候。</p>
<p>这时候因为没有在普通函数的内部进行定义，所以this会指向其他普通函数，或者全局对象上，导致bug！</p>
<ol start="2">
<li>回调函数的动态this</li>
</ol>
<p>下文是一个修改dom文本的操作，因为this指向错误，导致修改失败：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> button = <span class="built_in">document</span>.getElementById(<span class="string">'myButton'</span>);</span><br><span class="line">button.addEventListener(<span class="string">'click'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">this</span>.innerHTML = <span class="string">'Clicked button'</span>; <span class="comment">// this又指向了全局</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>相信你也知道了，改成普通函数就成了。</p>
<ol start="3">
<li><p>考虑代码的可读性，使用普通函数</p>
<ul>
<li><p>函数体复杂：</p>
<p>具体表现就是箭头函数中使用多个三元运算符号，就是不换行，非要在一行内写完，非常恶心！</p>
</li>
<li><p>行数较多</p>
</li>
<li>函数内部有大量操作</li>
</ul>
</li>
</ol>
<h2 id="文章内容小结："><a href="#文章内容小结：" class="headerlink" title="文章内容小结："></a>文章内容小结：</h2><h3 id="普通函数和箭头函数的区别：-1"><a href="#普通函数和箭头函数的区别：-1" class="headerlink" title="普通函数和箭头函数的区别："></a>普通函数和箭头函数的区别：</h3><ol>
<li>箭头函数没有<code>prototype</code>(原型)，所以箭头函数本身没有this</li>
<li>箭头函数的this在定义的时候继承自外层第一个普通函数的this。</li>
<li>如果箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向<code>window</code>(全局对象)</li>
<li>箭头函数本身的this指向不能改变，但可以修改它要继承的对象的this。</li>
<li>箭头函数的this指向全局，使用arguments会报未声明的错误。</li>
<li>箭头函数的this指向普通函数时,它的<code>argumens</code>继承于该普通函数</li>
<li>使用<code>new</code>调用箭头函数会报错，因为箭头函数没有<code>constructor</code></li>
<li>箭头函数不支持<code>new.target</code></li>
<li>箭头函数不支持重命名函数参数,普通函数的函数参数支持重命名</li>
<li>箭头函数相对于普通函数语法更简洁优雅</li>
</ol>
<h3 id="箭头函数的注意事项及不适用场景-1"><a href="#箭头函数的注意事项及不适用场景-1" class="headerlink" title="箭头函数的注意事项及不适用场景"></a>箭头函数的注意事项及不适用场景</h3><p><strong>箭头函数的注意事项</strong>：</p>
<ol>
<li>箭头函数一条语句返回对象字面量，需要加括号</li>
<li>箭头函数在参数和箭头之间不能换行</li>
<li>箭头函数的解析顺序相对<code>||</code>靠前</li>
</ol>
<p><strong>不适用场景</strong>：箭头函数的this意外指向和代码的可读性。</p>
<hr>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>呕心沥血，可以说是很全了，反正第一次问到我的时候只能想到箭头函数的this是继承而来的，以及语法上的简洁性，其他的我都不知道，希望这篇文章能够帮助各位同学学到知识。</p>
<p>PS：目前找工作中，求大佬们内推，中高级前端，偏JS，Vue，上海杨浦。</p>
<p><a href="http://obkoro1.com/" target="_blank" rel="noopener">博客</a>、<a href="http://obkoro1.com/web_accumulate/accumulate/" target="_blank" rel="noopener">前端积累文档</a>、<a href="https://raw.githubusercontent.com/OBKoro1/articleImg_src/master/juejin/1631b6f52f7e7015.jpeg?raw=true" target="_blank" rel="noopener">公众号</a>、<a href="https://github.com/OBKoro1" target="_blank" rel="noopener">GitHub</a>、wx:OBkoro1、邮箱：<a href="mailto:obkoro1@foxmail.com" target="_blank" rel="noopener">obkoro1@foxmail.com</a></p>
<p> 以上2019.03.22</p>
<p> 参考资料：</p>
<p><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions" target="_blank" rel="noopener">MDN 箭头函数</a></p>
<p><a href="http://es6.ruanyifeng.com/?search=new.target&amp;x=0&amp;y=0#docs/function#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0" target="_blank" rel="noopener">阮一峰-ES6入门</a></p>
<p><a href="https://zhuanlan.zhihu.com/p/26540168" target="_blank" rel="noopener">什么时候你不能使用箭头函数？</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>听说，打赏我的人最后都找到了真爱。</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/pay/weixin.jpg" alt="OBKoro1 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/pay/ali.jpg" alt="OBKoro1 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/js/" rel="tag"><i class="fa fa-tag"></i> js</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/02/19/H5-notification浏览器桌面通知/" rel="next" title="H5 notification浏览器桌面通知">
                <i class="fa fa-chevron-left"></i> H5 notification浏览器桌面通知
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/05/20/js-调用栈机制与ES6尾调用优化介绍/" rel="prev" title="js 调用栈机制与ES6尾调用优化介绍">
                js 调用栈机制与ES6尾调用优化介绍 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/uploads/hand1.jpg"
              alt="OBKoro1" />
          
            <p class="site-author-name" itemprop="name">OBKoro1</p>
            <p class="site-description motion-element" itemprop="description">种一棵树最好的时间是十年前,其次是现在</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives">
            
                <span class="site-state-item-count">105</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">74</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/OBKoro1" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" title="掘金">
                  
                    <i class="fa fa-fw fa-globe"></i>掘金</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://obkoro1.com/web_accumulate/" target="_blank" title="前端积累">
                  
                    <i class="fa fa-fw fa-globe"></i>前端积累</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://segmentfault.com/u/obkoro1/articles" target="_blank" title="segmentfault">
                  
                    <i class="fa fa-fw fa-globe"></i>segmentfault</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#普通函数和箭头函数的区别："><span class="nav-number">1.</span> <span class="nav-text">普通函数和箭头函数的区别：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数的this指向规则："><span class="nav-number">1.1.</span> <span class="nav-text">箭头函数的this指向规则：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-箭头函数没有prototype-原型-，所以箭头函数本身没有this"><span class="nav-number">1.1.1.</span> <span class="nav-text">1. 箭头函数没有prototype(原型)，所以箭头函数本身没有this</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-箭头函数的this指向在定义的时候继承自外层第一个普通函数的this。"><span class="nav-number">1.1.2.</span> <span class="nav-text">2. 箭头函数的this指向在定义的时候继承自外层第一个普通函数的this。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-不能直接修改箭头函数的this指向"><span class="nav-number">1.1.3.</span> <span class="nav-text">3. 不能直接修改箭头函数的this指向</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向window-全局对象"><span class="nav-number">1.1.4.</span> <span class="nav-text">4. 箭头函数外层没有普通函数，严格模式和非严格模式下它的this都会指向window(全局对象)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数的"><span class="nav-number">1.2.</span> <span class="nav-text">箭头函数的</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数的arguments"><span class="nav-number">1.3.</span> <span class="nav-text">箭头函数的arguments</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#箭头函数的this指向全局，使用arguments会报未声明的错误"><span class="nav-number">1.3.1.</span> <span class="nav-text">箭头函数的this指向全局，使用arguments会报未声明的错误</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#箭头函数的this指向普通函数时-它的argumens继承于该普通函数"><span class="nav-number">1.3.2.</span> <span class="nav-text">箭头函数的this指向普通函数时,它的argumens继承于该普通函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#rest参数获取函数的多余参数"><span class="nav-number">1.3.3.</span> <span class="nav-text">rest参数获取函数的多余参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用new调用箭头函数会报错"><span class="nav-number">1.3.4.</span> <span class="nav-text">使用new调用箭头函数会报错</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#箭头函数不支持new-target："><span class="nav-number">1.3.5.</span> <span class="nav-text">箭头函数不支持new.target：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数不支持重命名函数参数-普通函数的函数参数支持重命名"><span class="nav-number">1.4.</span> <span class="nav-text">箭头函数不支持重命名函数参数,普通函数的函数参数支持重命名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数相对于普通函数语法更简洁优雅："><span class="nav-number">1.5.</span> <span class="nav-text">箭头函数相对于普通函数语法更简洁优雅：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#箭头函数的注意事项及不适用场景"><span class="nav-number">2.</span> <span class="nav-text">箭头函数的注意事项及不适用场景</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数的注意事项"><span class="nav-number">2.1.</span> <span class="nav-text">箭头函数的注意事项</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数不适用场景："><span class="nav-number">2.2.</span> <span class="nav-text">箭头函数不适用场景：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文章内容小结："><span class="nav-number">3.</span> <span class="nav-text">文章内容小结：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#普通函数和箭头函数的区别：-1"><span class="nav-number">3.1.</span> <span class="nav-text">普通函数和箭头函数的区别：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#箭头函数的注意事项及不适用场景-1"><span class="nav-number">3.2.</span> <span class="nav-text">箭头函数的注意事项及不适用场景</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">4.</span> <span class="nav-text">结语</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2017 &mdash; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OBKoro1</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">
      156.0k
    </span>
  
</div>




<div>
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span class="site-uv"><i class="fa fa-user"></i> 访问用户： <span class="busuanzi-value" id="busuanzi_value_site_uv">21621</span> 人 </span>
<span class="site-pv"><i class="fa fa-eye"></i> 访问次数： <span class="busuanzi-value" id="busuanzi_value_site_pv">68503</span> 次</span>

</div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("9ANFp60h6oVyxIPGlau3NOA4-gzGzoHsz", "tMdNuKMbFtN4QsekeNVu83TV");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>

